//
//  YHView.swift
//  SniaWeiBo
//
//  Created by lcc on 16/5/11.
//  Copyright © 2016年 lcc. All rights reserved.
//

import UIKit
import SnapKit

class YHView: UIView {
    
    //设置闭包
    var closureBag:(()->())?
    //懒加载控件
    //旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //iconImageView
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文本信息
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(15)
        
        return label
        
    }()
    //注册按钮
    private lazy var registerButton:UIButton = {
    
        let button = UIButton()
        
        //点击事件
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()
    //登录按钮
    private lazy var loginButton:UIButton = {
        
        let button = UIButton()

        //点击事件
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置约束
        setUpUI()
    }
    
    //设置约束
    private func setUpUI() {
    
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        //添加视图
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //旋转图片
        cycleImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.size.equalTo(CGSize(width: 224, height: 60))

        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(30)
            make.leading.equalTo(messageLabel.snp_leading)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(30)
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.trailing.equalTo(messageLabel)
        }
        
//        startAnimation()
    }
    
    //创建访客视图
    func setVisitorInfo(title: String? , imageName: String?) {
    
        if let t = title,imgName = imageName {
            messageLabel.text = t
            iconImageView.image = UIImage(named: imgName)
            
            cycleImageView.hidden = true
        }else {
            
            cycleImageView.hidden = false
            
            //旋转动画
            startAnimation()
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //点击事件
   @objc private func registerAction() {
        print("registerButton")
    
        closureBag?()
    
    
    }
    
   @objc private func loginAction() {
        print("loginButton")
        
        closureBag?()
    }
    
    //动画
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
}
